Lighting Parts on Fire
In this tutorial we will learn how to use events and functions to create areas in the game that light parts on fire and put them out. =Setup= The first thing is to put in two parts. One part will be used to set parts that hit it on fire, and the other part will put out fires. In the Explorer, make sure one is called FirePart and the other is called WaterPart. We also need a Script in ServerScriptService. Right click on ServerScriptService, hover over Insert Object, and select Script. =Lighting Fires= We have been using functions quite a bit already: print and wait are functions that can work in any ROBLOX script, while SetMinutesAfterMidnight is a member function of the Lighting service. These functions have already been defined by ROBLOX, but we can create as many custom functions as we need to in our scripts. First, we will need a function to light a part on fire. We will start the function like this: function lightOnFire() print("Lighting on fire!") end There are several important parts to making a function. We have to start the line of code with the special keyword function. The next word will be the name of our function. The same rules for naming a variable applies to naming a function (no spaces, cannot start with a number, etc). Next, we add parentheses (). Then we add the code that we want to run when the function is called. In the above case, the code print("Lighting on fire!") will run when the function is called. After we put in all the code we want to run, we finish up with the keyword end. Our function isn't finished, but let's first try calling our function to test it. You'll notice that if you Run the game now, nothing will happen - the print statement that we wrote won't show up in the Output. That is because while we said what we want the function to do, we never actually called it. We can call this function just like we would call other functions: Using the name of the function followed by parentheses: function lightOnFire() print("Lighting on fire!") end lightOnFire() Now if we run the game Lighting on fire! will show up in the Output. Let's keep working on our function. Right now it only prints, and we want to make flames happen! We want to be able to use this function to light specific parts on fire, so next we need to make sure we can tell the function which part we want to light up. We do this by adding what is called a parameter to the function. In between the parentheses in the function definition, include the word part like this: function lightOnFire(part) print("Lighting this part on fire!") print(part.Name) end lightOnFire() The word part in the above code is a parameter for our function. We have been using parameters in other functions: In the wait function we have to provide a number as a parameter and in the print function we can provide a string or a number as a parameter. In this case, we are going to provide the Part that we want to set on fire. Inside the function, we can use part just as we would use a normal variable. Now let's add some fire. The easiest way to do this is to create a new Fire instance in code, and make its parent the part that was passed in. Just like how we can create a new Part with the line Instance.new("Part"), we can create fire with the line Instance.new("Fire"). We have to make sure to store the fire in a variable so we can set its Parent property. function lightOnFire(part) print("Lighting this part on fire!") print(part.Name) fire = Instance.new("Fire") fire.Parent = part end The last thing we need to do is call this function whenever a part hits the FirePart we created at the beginning of the tutorial. To do this, we are going to use something called an Event; Events are used to call functions when certain things happen in a game. Some examples of events are: when two things collide, when a player joins the game, and when a player presses a button; There are many places where we can call custom functions when events happen in the game. In this case, we are going to use an Event called Touched, which is associated with Parts: function lightOnFire(part) print("Lighting this part on fire!") print(part.Name) fire = Instance.new("Fire") fire.Parent = part end firePart = game.Workspace.firePart firePart.Touched:connect(lightOnFire) Let's test our game! Hit Play so we get a character and walk over the FirePart. Our character's legs will catch on fire as soon as they touch it! Putting Fires Out Let's be nice to our players and add a way for them to put out the fire by stepping on the WaterPart. Just like lighting a fire, we need to create a function to put out fires and then connect that function to the WaterPart's Touched event. function putOutFire(part) print("Removing Fire From:") print(part.Name) end waterPart = game.Workspace.waterPart water.Touched:connect(putOutFire) To get rid of fire we can use a function called Destroy. This function can be used on any object in ROBLOX to get rid of it. It is tempting to write the function just like this: function putOutFire(part) print("Removing Fire From:") print(part.Name) part.Fire:destroy() end waterPart = game.Workspace.waterPart water.Touched:connect(putOutFire) But there's a flaw in this code. What if we never had fire in the first place? The above code would give an error. To fix this, we would need to use FindFirstChild ''to detect if Fire actually exists. function putOutFire(part) print("Removing Fire From:") print(part.Name) fire = part:FindFirstChild("Fire") if Fire then fire:destroy() end end waterPart = game.Workspace.waterPart water.Touched:connect(putOutFire) In our function, after the prints, we first create a variable called ''fire, and in it we store the result of FindFirstChild("Fire"). The next line, if fire then, is a conditional check to see if anything was stored in fire. If FindFirstChild successfully stored something inside of the fire variable, then the next line, fire:Destroy() will be called. Otherwise, it won't do anything because nothing was found. This way we don't have any errors when we call the function. Now we can test our game again with Play. We can now light our character on fire by stepping on the FirePart, and then douse the flames by stepping on the WaterPart. Next Steps Let's combine our new code with some terrain! We can have a player light on fire if they step on lava, and they can put it out by jumping in water. This is much more interesting and immersive than simply stepping on blocks. First, lets add a bit of terrain using the Add terrain tool: Move and scale the fire and water parts that we made earlier so that they completely overlap the terrain we just added. Make sure both parts are Anchored and that their Script error Property is off. You can also make them translucent with their Transparency properties to help position them. Lastly, to the end of our script we will make the parts completely see through so they don't appear in game: firePart.Transparency = 1 waterPart.Transparency = 1 Now when we Play our game, we can jump onto the lava to light on fire and jump in the water to put it out! Here's the code that we created function lightOnFire(part) print("Lighting this part on fire:") print(part.Name) fire = Instance.new("Fire") fire.Parent = part end firePart = game.Workspace.FirePart firePart.Touched:connect(lightOnFire) function putOutFire(part) print("Removing fire from:") print(part.Name) fire = part:FindFirstChild("Fire") if fire then fire:Destroy() end end waterPart = game.Workspace.WaterPart waterPart.Touched:connect(putOutFire) firePart.Transparency = 1 waterPart.Transparency = 1 Category:Scripting Category:Tutorials